import 'package:flutter/material.dart';
import 'package:yuan_hang_app/base/full_base_page.dart';
import 'package:yuan_hang_app/res/w_colors.dart';
import 'package:yuan_hang_app/utils/size_utils.dart';
import '../../../../base/base_state.dart';
import '../../../../http/api.dart';
import '../../../../http/http_utils.dart';
import '../../../../res/w_constant.dart';
import '../../../../utils/string_utils.dart';
import '../../../../utils/common_req_utils.dart';
import '../../../../widget/label_item.dart';
import '../../../../widget/w_toast.dart';

class DriveFeedBackDetailPage extends StatefulWidget {
  int feedbackId;

  DriveFeedBackDetailPage({super.key, required this.feedbackId});

  @override
  BaseState<DriveFeedBackDetailPage> createState() =>
      _DriveFeedBackDetailPageState();
}

class _DriveFeedBackDetailPageState extends BaseState<DriveFeedBackDetailPage> {
  final _formKey = GlobalKey<FormState>();
  List<Map> driveStatus =
      CommonReqUtils.getLabelListByKey(CommonReqUtils.driveStatus);
  List<Map> modelList =
      CommonReqUtils.getLabelListByKey(CommonReqUtils.carType);
  List<Map> saleManList = WConstant().salesmanList;
  GlobalKey<LabelItemState>? driveExperienceKey;
  GlobalKey<LabelItemState>? modelKey;

  Map driveFeedBackDetail = {};

  @override
  FullBasePage buildState(BuildContext context) {
    return FullBasePage(
        title: "试驾回访详情",
        resizeToBottomInset: true,
        child: SingleChildScrollView(
            padding: EdgeInsets.zero, child: _bookDriveItem()));
  }

  Widget _bookDriveItem() {
    return Form(
        key: _formKey,
        child: Container(
            alignment: Alignment.center,
            margin: EdgeInsets.only(
                left: 10.px, right: 10.px, bottom: 10.px, top: 10.px),
            decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(10.px),
                color: Colors.white,
                boxShadow: const [
                  BoxShadow(
                      //线框阴影线条
                      color: WColors.border,
                      offset: Offset(0, 1),
                      blurRadius: 2)
                ]),
            child: Column(
                mainAxisAlignment: MainAxisAlignment.start,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  LabelItem(
                    label: "客户名称",
                    value: StringUtils.safeGetValue(
                        driveFeedBackDetail["customerName"]),
                    //   isEnabled: false,
                  ),
                  LabelItem(
                    label: "手机号码",
                    value:
                        StringUtils.safeGetValue(driveFeedBackDetail["phone"]),
                  ),
                  LabelItem(
                      label: "试驾类型",
                      //isMust: true,
                      labelType: LabelType.TEXT,
                      value: StringUtils.safeGetValue(
                          driveFeedBackDetail["driveTypeName"],
                          placeHolder: "-")),
                  LabelItem(
                      label: "试驾方式",
                      //  isMust: true,
                      labelType: LabelType.TEXT,
                      value: StringUtils.safeGetValue(
                          driveFeedBackDetail["driveMethodName"],
                          placeHolder: "-")),
                  LabelItem(

                    label: "试驾车型",
                    value: CommonReqUtils.exchangeCodeToName2(
                        modelList, driveFeedBackDetail["modelId"]),
                  ),
                  LabelItem(

                    label: "开始时间",
                    value: StringUtils.safeGetValue(
                        driveFeedBackDetail["startDriveTime"],
                        placeHolder: "-"),
                  ),
                  LabelItem(
                    label: "结束时间",
                    value: StringUtils.safeGetValue(
                        driveFeedBackDetail["endDriveTime"],
                        placeHolder: "-"),
                  ),
                  LabelItem(
                      label: "接待顾问",
                      value: StringUtils.safeGetValue(
                          driveFeedBackDetail["saleMan"],
                          placeHolder: "未知")),
                  LabelItem(
                      label: "试驾状态",
                      //isMust: true,
                      labelType: LabelType.TEXT,
                      value: StringUtils.safeGetValue(
                          driveFeedBackDetail["statusName"],
                          placeHolder: "-")),
                  LabelItem(
                    label: "回访时间",
                    value: StringUtils.safeGetValue(
                        driveFeedBackDetail["probeTime"],
                        placeHolder: "-"),
                  ),
                  LabelItem(
                    labelType: LabelType.RATINGBAR,
                    label: "试驾体验",
                    isReadOnly: true,
                    value: StringUtils.safeGetValue(driveFeedBackDetail["testDriveExperience"])??"0.0",
                  ),
                  LabelItem(
                    labelType: LabelType.RATINGBAR,
                    label: "销售服务",
                    isReadOnly: true,
                    value: StringUtils.safeGetValue(driveFeedBackDetail["salesService"])??"0.0",
                  ),
                  LabelItem(
                    key: driveExperienceKey,
                    labelType: LabelType.TEXTAREA,
                    label: "试驾评价",
                    isEnabled: false,
                    value: StringUtils.safeGetValue(
                        driveFeedBackDetail["driveExperience"],
                        placeHolder: "无"),
                  ),
                ])));
  }

  void _printFormData() {
    print('详情信息对象是: $driveFeedBackDetail');

    // 如果需要，你可以在这里添加提交表单的逻辑，例如通过网络请求发送数据到服务器
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    driveExperienceKey = GlobalKey(debugLabel: "driveExperience");
    modelKey = GlobalKey(debugLabel: "modelKey");
    showLoading();
    HttpUtils()
        .get("${Api.getTestDriveDetail}/${widget.feedbackId}")
        .then((data) {
      hideLoading();
      if (data.resultCode == 200) {
        driveFeedBackDetail = data.data;
        driveFeedBackDetail["driveTypeName"] =
            CommonReqUtils.exchangeCodeToName(CommonReqUtils.driveType,
                StringUtils.safeGetValue(driveFeedBackDetail["driveType"]));
        driveFeedBackDetail["driveMethodName"] =
            CommonReqUtils.exchangeCodeToName(CommonReqUtils.driveMethod,
                StringUtils.safeGetValue(driveFeedBackDetail["driveMethod"]));
        driveFeedBackDetail["statusName"] = CommonReqUtils.exchangeCodeToName(
            CommonReqUtils.driveStatus,
            StringUtils.safeGetValue(driveFeedBackDetail["status"]));
        setState(() {});
        driveExperienceKey!.currentState
            ?.refreshInput(driveFeedBackDetail["driveExperience"] ?? "");
        modelKey!.currentState
            ?.refreshList(modelList, driveFeedBackDetail["modelId"]);
      } else if (data.resultCode == 500) {
        Toast.toast(data.errMsg ?? "");
      }
    }).catchError((e) {
      hideLoading();
    });
  }
}
